/**
 *  @Package: com.yidu.drug.service.impl
 *  @author: Administrator
 *  @date: 2018年8月9日 下午2:29:52
 */
package com.yidu.drug.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.yidu.drug.common.Page;
import com.yidu.drug.common.Tools;
import com.yidu.drug.dao.DrugManagerModelMapper;
import com.yidu.drug.dao.DrugManagerRoleModelMapper;
import com.yidu.drug.dao.DrugModuleModelMapper;
import com.yidu.drug.dao.DrugRoleModelMapper;
import com.yidu.drug.model.DrugManagerModel;
import com.yidu.drug.model.DrugManagerRoleModel;
import com.yidu.drug.model.DrugModuleModel;
import com.yidu.drug.model.DrugRoleModel;
import com.yidu.drug.service.DrugManagerService;

/**
 * 类的描述 实现用户service接口
 * @author 祝浪
 * @date 2018年8月9日
 */
@Service
public class DrugManagerServiceImpl implements DrugManagerService {

	@Resource
	DrugManagerModelMapper manDao;
	@Resource
	DrugModuleModelMapper modDao;
	@Resource
	DrugManagerRoleModelMapper manRoDao;
	@Resource
	DrugRoleModelMapper rolDao;
	@Override
	public void addOrUpdate(DrugManagerModel us) {
		
		if(!Tools.isEmpty(us.getManId())){
			manDao.updateByPrimaryKeySelective(us);
			manRoDao.deleteByUsId(us.getManId());
		}else{
			String usId = Tools.getRandomUUID();
			us.setManId(usId);
			
			manDao.insertSelective(us);
		}
		if(!Tools.isEmpty(us.getRoleIds())){
			String[] role = us.getRoleIds().split("-");
			for (String string : role) {
				DrugManagerRoleModel mr = new DrugManagerRoleModel();
				String mrId = Tools.getRandomUUID();	
				mr.setMrId(mrId);
				mr.setManId(us.getManId());
				mr.setRoleId(string);
				manRoDao.insertSelective(mr);
			}
		}
	}

	
	@Override
	public void delete(DrugManagerModel us) {
		manRoDao.deleteByUsId(us.getManId());
		manDao.deleteByPrimaryKey(us.getManId());
	}
	@Override
	public List<DrugManagerModel> findLimit(DrugManagerModel us, Page page) {
		DrugManagerModel t = new DrugManagerModel();
		BeanUtils.copyProperties(us, t);
		List<DrugManagerModel> poList = manDao.findLimit(t, page);
		for (DrugManagerModel userStore : poList) {
			if(userStore.getIsva()==0){
				userStore.setIsvas("已审核");
			}else{
				userStore.setIsvas("未审核");
			}
			userStore.setOptimes(Tools.SDF_TIME.format(userStore.getOptime()));
		}
		return poList;
	}


	@Override
	public Integer findTotalRows(DrugManagerModel vo) {
		
		int row = manDao.findTotalRows(vo);
		return row;
	}
	@Override
	public List<DrugRoleModel> findRole(DrugManagerModel vo) {
		List<DrugRoleModel> roleList = rolDao.selectAll();
		
		return roleList;
	}


	@Override
	public List<DrugManagerRoleModel> findRoleByManId(String manId) {
		List<DrugManagerRoleModel> maRoList = manRoDao.findRoleByManId(manId);
		List<DrugManagerRoleModel> list = new ArrayList<DrugManagerRoleModel>();
		for (DrugManagerRoleModel ehyManagerRoleEntity : maRoList) {
			
			
			list.add(ehyManagerRoleEntity);
		}
		return list;
	}
	@Override
	public DrugManagerModel isLogin(DrugManagerModel us) {
		
		DrugManagerModel user = manDao.selectUser(us);
		if(user!=null){
			
			List<DrugModuleModel> modList = modDao.findMoudelByManId(user.getManId());
			user.setRoleName(modList.get(0).getRoleName());
			
			List<DrugModuleModel> moVoList = new ArrayList<DrugModuleModel>();
			for (DrugModuleModel ehyModule :modDao.findByPid("main")) {
				
				moVoList.add(ehyModule);
			}
			List<DrugModuleModel> modAll = getTypes(moVoList);
			user.setChildren(getManagerHasModule(modAll, modList));
			return user;
		}
		return null;
	}
	
	/**
	 * 查询所有模块
	 * @param modAll
	 * @param hasMods
	 * @return
	 */
	public List<DrugModuleModel> getManagerHasModule(List<DrugModuleModel> modAll, List<DrugModuleModel> hasMods){
		List<DrugModuleModel> groupMod = new ArrayList<>();
		for(DrugModuleModel mod:modAll) {
			if(mod.getChildren().isEmpty()) {
				for(DrugModuleModel hasMod:hasMods) {

					if(hasMod.getModId().equals(mod.getModId())) {
						groupMod.add(mod);
						
					}
				}
			}else {
				List<DrugModuleModel> reGroupMod = getManagerHasModule(mod.getChildren(), hasMods);
				if(!reGroupMod.isEmpty()) {
					mod.setChildren(reGroupMod);
					groupMod.add(mod);
				}
			}
		}
		return groupMod;
	}

	/**
	 * 递归查询类型的子类型
	 * 
	 * @param types 类型
	 * @return
	 */
	public List<DrugModuleModel> getTypes(List<DrugModuleModel> mods) {
		// 循环遍历传入的集合
		for (DrugModuleModel mod : mods) {
			// 取到类型的父ID
			String pid = mod.getModId();
			// 根据类型的父ID查询类型集合
			List<DrugModuleModel> modc = new ArrayList<DrugModuleModel>();
			for (DrugModuleModel ehyModuleEntity : modDao.findByPid(pid)) {
				
				modc.add(ehyModuleEntity);
			}
			// 给类型对象设置子级
			mod.setChildren(modc);
			// 如果类型集合大小大于0则将自己的子级作为参数 调用自己
			if (modc.size() > 0) {
				getTypes(modc);
			}
		}
		// 返回参数
		return mods;
	}
	/**
	 * 修改方法
	 * 将用户的所有角色从新加
	 */
	@Override
	public void updateVet(DrugManagerModel us) {
		
		manDao.updateByPrimaryKeySelective(us);
		DrugManagerRoleModel usRo = new DrugManagerRoleModel();
		usRo.setMrId(Tools.getRandomUUID());
		usRo.setRoleId("shopowner");
		usRo.setManId(us.getManId());
		manRoDao.insertSelective(usRo);
	}

	/**
	 * 根据用户id查询
	 */
	@Override
	public DrugManagerModel findByUsId(String usId) {
		DrugManagerModel us = manDao.selectByPrimaryKey(usId);
		
		return us;
	}

	/**
	 * 修改
	 */
	@Override
	public void updateUser(DrugManagerModel usVo) {
		
		manDao.updateByPrimaryKeySelective(usVo);
	}


	@Override
	public List<DrugRoleModel> findRoleByStaff() {
		List<DrugRoleModel> roleList = rolDao.selectAll();
		List<DrugRoleModel> ehyRoleEntityVo = new ArrayList<DrugRoleModel>();
		for (DrugRoleModel ehyRoleEntity : roleList) {
			if(!Tools.isEmpty(ehyRoleEntity.getRoleCode())){
				
				ehyRoleEntityVo.add(ehyRoleEntity);
			}
			
		}
		return ehyRoleEntityVo;
	}
	
}
